package com.clps.loadDictory;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@Component
public class EnumDictionaryLoader implements ApplicationRunner {

    private static final String KEY_PREFIX = "ENUM_DICTIONARY";

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Resource resource = new ClassPathResource("ENUM_DICTIONARY.json");
        String filename = getFileNameWithoutExtension(resource);
        List<String> lines = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
            String line;
            while ((line = reader.readLine()) != null) {
                lines.add(line);
            }
        }

        // 此时lines列表包含了文件的所有行
        String content = String.join("\n", lines); // 将所有行合并成一个字符串，行与行之间用\n分隔
        System.out.println("filename = " + filename);
        // 你可以在这里使用content变量，比如打印出来或进一步处理
        System.out.println(content);
    }

    private String getFileNameWithoutExtension(Resource resource) {
        String fileName = resource.getFilename();
        assert fileName != null;
        if (fileName.contains(".")) {
            return fileName.substring(0, fileName.lastIndexOf('.'));
        }
        return fileName; // 如果没有扩展名，直接返回文件名
    }

}
